unit Verifier;

interface

uses
   SysUtils,Controls,Dialogs,Afficher, Bot;


Type Tablo = Array Of String;

Function VerifierMotSimule(Mot : Tablo): Boolean;

Function VerifierEntrer(Mot : String; Horizontal: Boolean; DebutX, DebutY : byte): Boolean;

Function VerifierMot(Mot : string): Boolean;

Function MotAlentour(Mot : String;Hor : boolean; X,Y : byte) : Tablo;

Procedure AjouterMot(Mot : string);

Function VerifierLettresF(Mot, Lettres : String; Hor : Boolean; X,Y : byte): Boolean;

Function Verifier1erTour(Size, X, Y : byte; Hor : boolean) : boolean;

Function VerifierPos(Size,X,Y : Byte; hor : boolean) : Boolean;

Function VerifierNouveauMot(Size,X,Y : Byte; Hor : boolean): Boolean;

Function VerifierEntrerSimule(Mot : String; Horizontal : Boolean; DebutX, DebutY : byte): Boolean;


implementation


Function VerifierMot(Mot : string): Boolean;
Var Fichier : TextFile;
    Str : String;
    I : Integer;
Begin
  Mot := LowerCase(Mot);
  Result := False;

  For I := 0 To High(WordsListe[Length(Mot)]) Do
    Begin
      Str := Bot.WordsListe[Length(Mot)][I];
      If Mot = Str Then
        Begin                             
          Result := true;
          Break;
        End;
    End;


End;



Procedure AjouterMot(Mot : string);
Var Fichier : TextFile;
Begin
  AssignFile(Fichier,'words.in');
  Append(Fichier);
  WriteLn(Fichier,Mot);
  CloseFile(Fichier);
  SetLength(Bot.WordsListe, High(Bot.wordsListe) + 2);
  Bot.WordsListe[Length(Mot)][High(Bot.WordsListe[Length(Mot)]) - 1] := Mot;
End;

Function MotAlentour(Mot : String;Hor : boolean; X,Y : byte) : Tablo;
Var I,J,Z,k, cpt : byte;
    str : string;
Begin

cpt := 0;

Mot := UpperCase(Mot);

SetLength(result,Length(Mot) + 1);
IF Hor Then
  For I := 1 To Length(mot) Do
    Begin
      IF Plateau[X + I - 1, Y] = '' Then
        Begin
          Str := '';
          J := 1;
          While Afficher.Plateau[X + I - 1, Y + J] <> '' Do Inc(j);
          Z := 1;
          While Afficher.Plateau[X + I - 1, Y - Z] <> '' Do Inc(Z);
          IF (Y - Z + 1) <> (Y + J - 1) Then
          For k := (Y - Z + 1) To (Y + J - 1) Do
            Begin
              IF K <> Y Then
                Str := Str + afficher.plateau[X + I - 1, k]
              Else
                Str := Str + Mot[I];
            End;
          IF Str <> '' Then
            Begin
              Inc(cpt);
              Result[cpt - 1] := Str;
            End;
        End;
    End
Else
  For I := 1 To Length(mot) Do
    Begin
      IF Plateau[X, Y + I - 1] = '' Then
        Begin
          Str := '';
          J := 1;
          While Afficher.Plateau[X + J, Y + I - 1] <> '' Do Inc(j);
          Z := 1;
          While Afficher.Plateau[X - Z, Y + I - 1] <> '' Do Inc(Z);

          IF (X - Z + 1) <> (X + J - 1) Then
            For k := (X - Z + 1) To (X + J - 1) Do
              Begin
                IF K <> X Then
                  Str := Str + afficher.plateau[k, Y + I - 1]
                Else
                  Str := Str + Mot[I];
              End;
          IF Str <> '' Then
            Begin
              Inc(cpt);
              Result[cpt - 1] := Str;
            End;
        End;
    End;

End;


Function VerifierEntrer(Mot : String; Horizontal : Boolean; DebutX, DebutY : byte): Boolean;
Var MotStr : Tablo;
    I : Byte;
Begin

Result := true;

MotStr := MotAlentour(Lowercase(mot),Horizontal,DebutX,DebutY);



IF Not VerifierMot(LowerCase(mot)) Then
    If MessageDlg(UpperCase(mot) + ' fait pas parti de la liste de mots. Voulez vous l''y ajouter?',
                mtInformation,[mbYes, mbNo],0) = MrYes Then
      AjouterMot(LowerCase(mot))
    Else
      Result := False
Else
  IF Not VerifierMot(LowerCase(mot)) Then
    Begin
      Result := False;
      Exit;
    End;

For I := 0 To Length(mot) - 1 Do
  Begin
    IF MotStr[I] <> '' Then
        IF Not VerifierMot(MotStr[I]) Then
          If MessageDlg(UpperCase(MotStr[I]) + ' ne fait pas parti de la liste de mots. Voulez vous l''y ajouter?',
                     mtInformation,[mbYes, mbNo],0) = MrYes Then
            AjouterMot(LowerCase(MotStr[I]))
          Else
            Result := False
    Else
      Begin
        Result := True;
        Exit;
      End;

    End;


End;



Function VerifierLettresF(Mot, Lettres : String; Hor : Boolean; X,Y : byte): Boolean;
Var CurStr : String;
    I, J : Byte;
Begin

Result := true;
J := 0;

IF Hor Then
  For I := X To X +Length(Mot) - 1 Do
    Begin
      Inc(j);
      IF (Plateau[I, Y] = '')  Then
          IF Pos(Mot[J],Lettres) <> 0 Then
            Delete(Lettres,Pos(Mot[J],Lettres),1)
          Else
            Begin
              Result := False;
            End
      Else
        IF (Plateau[I,Y] <> Mot[J])  Then
          Begin
            Result := False;
            Exit;
          End;
    End

Else
  For I := Y To Y + Length(Mot) - 1 Do
    Begin
      Inc(j);
      IF (Plateau[X, I] = '')  Then
          IF Pos(Mot[J],Lettres) <> 0 Then
            Delete(Lettres,Pos(Mot[J],Lettres),1)
          Else
            Begin
              Result := False;
            End
      Else
        IF (Plateau[X,I] <> Mot[J]) Then
          Begin
            Result := False;
            Exit;
          End;
    End;

End;


Function Verifier1erTour(Size, X, Y : byte; Hor : boolean) : boolean;
var I : Byte;
Begin

Result := False;

If Hor Then
  For I := X To X + Size - 1 Do
    Begin
      IF (Y = 8) AND (I = 8) Then
        Begin
          Result := true;
          Exit;
        End;
    End
Else
  For I := Y To Y + Size - 1 Do
    Begin
      IF (X = 8) AND (I = 8) Then
        Begin
          Result := true;
          Exit;
        End;
    End;
End;



Function VerifierPos(Size,X,Y : Byte; hor : boolean) : Boolean;
var I : Byte;
Begin

Result := False;

If Hor Then
  For I := X To X + Size - 1 Do
    Begin
      IF (Afficher.Plateau[I,Y] <> '') Or (Afficher.Plateau[I,Y + 1] <> '') Or (Afficher.Plateau[I,Y - 1] <> '') Then Result := True;
      IF (Afficher.Plateau[X - 1, Y] <> '') OR (Afficher.Plateau[X + Size, Y] <> '') Then
      Begin
        Result := False;
        Exit;
      End;
    End
Else
  For I := Y To Y + Size - 1 Do
    Begin
      IF (Afficher.Plateau[X,I] <> '') Or (Afficher.Plateau[X + 1,I] <> '') Or (Afficher.Plateau[X - 1,I] <> '') Then Result := True;

      IF (Afficher.Plateau[X, Y - 1] <> '') OR (Afficher.Plateau[X, Y + Size] <> '') Then
      Begin
        Result := False;
        Exit;
      End;
    End;

End;

Function VerifierNouveauMot(Size,X,Y : Byte; Hor : boolean): Boolean;
Var i : Byte;
Begin

Result := False;

If Hor then
  For I := X TO X + Size - 1 Do
    Begin
      If Afficher.Plateau[I,Y] = '' Then
        Begin
          Result := True;
          Exit;
        End;
    End
Else
  For I := Y TO Y + Size - 1 Do
    Begin
      If Afficher.Plateau[X,I] = '' Then
        Begin
          Result := True;
          Exit;
        End;
    End;

End;




Function VerifierEntrerSimule(Mot : String; Horizontal : Boolean; DebutX, DebutY : byte): Boolean;
Var MotStr, MotStrBon : Tablo;
    I, J : Integer;
Begin

Result := true;

For I := 0 to High(MotStr) Do MotStr[I] := '';

MotStr := MotAlentour(Lowercase(mot),Horizontal,DebutX,DebutY);
SetLength(MotStr,High(MotStr) + 4);
MotStr[High(MotStr) - 2] := Mot;

SetLength(MotStrBon, 1000);
J := 0;
For I := 0 To High(MotStr) - 1 Do
  Begin
    IF MotStr[I] <> '' Then
      Begin
        MotStrBon[J] := MotStr[I];
        Inc(J);
      End;
  End;

MotStr := Nil;

SetLength(MotStrBon,J + 1);



IF Not VerifierMotSimule(MotStrBon) Then
  Begin
    Result := False;
    Exit;
  End;


End;





Function VerifierMotSimule(Mot : Tablo): Boolean;
Var Fichier : TextFile;
    Str : String;
    I, J : Integer;
    Bool : Array Of Boolean;
Begin
  For I := 0 To High(Mot) Do
    Begin
      Mot[I] := LowerCase(Mot[I]);
    End;
  SetLength(Bool, High(Mot) + 1);

  For I := 0 To High(Mot) Do
    Begin
      IF Mot[I] = '' Then
        Begin
          Bool[I] := True;
          Break;
        End;
      Bool[I] := False;
    End;



      For J := 0 To High(Mot) Do
        Begin
          For I := 0 To High(Bot.WordsListe[Length(Mot[J])]) Do
            Begin
              If Mot[J] = Bot.WordsListe[Length(Mot[J])][I] Then Bool[J] := true;
            End;
        End;

  Result := True;

  For I := 0 To High(Bool) Do
    Begin
      IF Not Bool[I] Then
        Begin
          Result := False;
          Break;
        End;
    End;




End;



end.
